package com.ruoyi.business.controller;

import java.util.List;

import com.ruoyi.business.domain.JtProductTraceability;
import com.ruoyi.business.service.IJtProductTraceabilityService;
import com.ruoyi.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.business.domain.JtTraceabilityReport;
import com.ruoyi.business.service.IJtTraceabilityReportService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 产品报告Controller
 * 
 * @author Alan Zhang
 * @date 2025-05-22
 */
@Controller
@RequestMapping("/traceability/report")
public class JtTraceabilityReportController extends BaseController
{
    private String prefix = "traceability/report";

    @Autowired
    private IJtTraceabilityReportService jtTraceabilityReportService;
    @Autowired
    private IJtProductTraceabilityService jtProductTraceabilityService;

    @RequiresPermissions("traceability:report:view")
    @GetMapping()
    public String report()
    {
        return prefix + "/report";
    }

    /**
     * 查询产品报告列表
     */
    @RequiresPermissions("traceability:report:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(JtTraceabilityReport jtTraceabilityReport)
    {
        startPage();
        List<JtTraceabilityReport> list = jtTraceabilityReportService.selectJtTraceabilityReportList(jtTraceabilityReport);
        return getDataTable(list);
    }

    /**
     * 导出产品报告列表
     */
    @RequiresPermissions("traceability:report:export")
    @Log(title = "产品报告", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(JtTraceabilityReport jtTraceabilityReport)
    {
        List<JtTraceabilityReport> list = jtTraceabilityReportService.selectJtTraceabilityReportList(jtTraceabilityReport);
        ExcelUtil<JtTraceabilityReport> util = new ExcelUtil<JtTraceabilityReport>(JtTraceabilityReport.class);
        return util.exportExcel(list, "产品报告数据");
    }

    /**
     * 新增产品报告
     */
    @RequiresPermissions("traceability:report:add")
    @GetMapping("/add")
    public String add()
    {
        return prefix + "/add";
    }

    /**
     * 新增保存产品报告
     */
    @RequiresPermissions("traceability:report:add")
    @Log(title = "产品报告", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(JtTraceabilityReport jtTraceabilityReport)
    {
        return toAjax(jtTraceabilityReportService.insertJtTraceabilityReport(jtTraceabilityReport));
    }

    /**
     * 修改产品报告
     */
    @RequiresPermissions("traceability:report:edit")
    @GetMapping("/edit/{reportId}")
    public String edit(@PathVariable("reportId") Long reportId, ModelMap mmap)
    {
        JtTraceabilityReport jtTraceabilityReport = jtTraceabilityReportService.selectJtTraceabilityReportByReportId(reportId);
        mmap.put("jtTraceabilityReport", jtTraceabilityReport);
        return prefix + "/edit";
    }

    /**
     * 修改保存产品报告
     */
    @RequiresPermissions("traceability:report:edit")
    @Log(title = "产品报告", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(JtTraceabilityReport jtTraceabilityReport)
    {
        return toAjax(jtTraceabilityReportService.updateJtTraceabilityReport(jtTraceabilityReport));
    }

    /**
     * 删除产品报告
     */
    @RequiresPermissions("traceability:report:remove")
    @Log(title = "产品报告", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(jtTraceabilityReportService.deleteJtTraceabilityReportByReportIds(ids));
    }


    /**
     * 产品溯源页面
     * @param batchCode
     * @param mmap
     * @return
     */
    @GetMapping("/view/{batchCode}")
    public String report(@PathVariable("batchCode") String batchCode, ModelMap mmap)
    {
        logger.info("产品溯源：{}", batchCode);
        JtProductTraceability param = new JtProductTraceability();
        param.setProductionBatch(batchCode);
        List<JtProductTraceability> list = jtProductTraceabilityService.selectJtProductTraceabilityList(param);
        if(list.size() > 0){
            JtProductTraceability entity = list.get(0);

            List<JtTraceabilityReport> reportList = jtTraceabilityReportService.selectJtTraceabilityReportById(entity.getTraceabilityId());
            entity.setReportList(reportList);
            mmap.put("productTraceability", entity);
        } else {
            mmap.put("resultMsg", "无效溯源码："+ batchCode);
        }
        return prefix + "/view";
    }
}
